// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_FIN_ACCOUNTING_VOUCHER
// * CreateDate    : 2012/2/2 10:17:12
// * Creater       : CodeSmith
// * LastChangeDate: 2012/2/2 10:17:12
// * LastChanger   : 2012/2/2 10:17:12
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.Business.Fin
{
/// <summary>
    /// FinAccountingVoucher
    /// </summary>
    public partial class FinAccountingVoucherBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objFinAccountingVoucherEntity">Entity</param>
        /// <returns>Entity</returns>
        public static FinAccountingVoucherEntity GetFinAccountingVoucher(FinAccountingVoucherEntity objFinAccountingVoucherEntity)
        {
            return DataAccess.SelectSingle<FinAccountingVoucherEntity>(objFinAccountingVoucherEntity);
        }
        public static FinAccountingVoucherEntity GetByID(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return null;
            }
            FinAccountingVoucherEntity entity = new FinAccountingVoucherEntity();
            entity.VoucherId = id;
            return DataAccess.SelectSingle<FinAccountingVoucherEntity>(entity);
        }
        public static DataTable GetByCondition(string condition, string orderBy, string rowCount)
        {
            string sql = CommonFunctions.GetSelect(rowCount) + @"AV.*, CP.NAME_CN AS CREATED_PERSON, AP.NAME_CN AS APPROVED_PERSON
                            FROM FIN_ACCOUNTING_VOUCHER AV
                            LEFT JOIN MST_PERSON CP ON CP.PERSON_ID = AV.CREATED_BY
                            LEFT JOIN MST_PERSON AP ON AP.PERSON_ID = AV.APPROVED_BY
                            WHERE 1=1 ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            if (string.IsNullOrEmpty(orderBy) == false)
            {
                sql += " ORDER BY " + orderBy;
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
        #endregion

        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objFinAccountingVoucherEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertFinAccountingVoucher(FinAccountingVoucherEntity objFinAccountingVoucherEntity)
        {
            return DataAccess.Insert(objFinAccountingVoucherEntity);
        }

        public static bool InsertFinAccountingVoucher(List<FinAccountingVoucherEntity> objFinAccountingVoucherEntityList)
        {
            return DataAccess.Insert(objFinAccountingVoucherEntityList);
        }
        #endregion

        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objFinAccountingVoucherEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateFinAccountingVoucher(FinAccountingVoucherEntity objFinAccountingVoucherEntity)
        {
            return DataAccess.Update(objFinAccountingVoucherEntity);
        }

        public static bool UpdateFinAccountingVoucher(List<FinAccountingVoucherEntity> objFinAccountingVoucherEntityList)
        {
            return DataAccess.Update<FinAccountingVoucherEntity>(objFinAccountingVoucherEntityList);
        }
        #endregion

        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objFinAccountingVoucherEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteFinAccountingVoucher(FinAccountingVoucherEntity objFinAccountingVoucherEntity)
        {
            return DataAccess.Delete(objFinAccountingVoucherEntity);
        }

        public static bool DeleteFinAccountingVoucher(List<FinAccountingVoucherEntity> objFinAccountingVoucherEntityList)
        {
            return DataAccess.Delete<FinAccountingVoucherEntity>(objFinAccountingVoucherEntityList);
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetFinAccountingVoucherAll()
        {
            return DataAccess.SelectAllDataSet<FinAccountingVoucherEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<FinAccountingVoucherEntity> GetFinAccountingVoucherAllList()
        {
            return DataAccess.SelectAll<FinAccountingVoucherEntity>();
        }
        #endregion

        #region Functions
        public static bool SaveAccountingVoucher(bool isNew, FinAccountingVoucherEntity account, DataTable dtDetail, List<FinAccountingVoucherItemEntity> deleteList)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (isNew)
                    {
                        DataAccess.Insert(account, broker);
                    }
                    else
                    {
                        DataAccess.Update(account, broker);
                    }

                    //Save items
                    List<FinAccountingVoucherItemEntity> insertList = new List<FinAccountingVoucherItemEntity>();
                    List<FinAccountingVoucherItemEntity> modifyList = new List<FinAccountingVoucherItemEntity>();
                    foreach (DataRow dr in dtDetail.Rows)
                    {
                        FinAccountingVoucherItemEntity entity = null;
                        switch (dr.RowState)
                        {
                            case DataRowState.Added:
                                entity = new FinAccountingVoucherItemEntity(dr);
                                insertList.Add(entity);
                                break;
                            case DataRowState.Modified:
                                entity = new FinAccountingVoucherItemEntity(dr);
                                modifyList.Add(entity);
                                break;
                            default:
                                continue;
                        }
                    }
                    DataAccess.Delete<FinAccountingVoucherItemEntity>(deleteList, broker);
                    DataAccess.Update<FinAccountingVoucherItemEntity>(modifyList, broker);
                    DataAccess.Insert<FinAccountingVoucherItemEntity>(insertList, broker);

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        #endregion
	}
	
	
}	

